const root = {
    val: 'A',
    left: {
        val: 'B',
        left: {
            val: 'D'
        },
        right: {
            val: 'E'
        },
    },
    right: {
        val: 'C',
        left: {
            val: 'F',
        }
    }
}


var zigzagLevelOrder = function (root) {
    if (!root) return []
    const queue = []
    queue.push(root)
     const res = []
     let level=0
    while (queue.length) {
        const arr = []
        const len=queue.length
        for (let i = 0; i < len; i++) {        
            const top = queue.shift()
            arr.push(top.val)
            if (top.left) {
                queue.push(top.left)
            }
            if (top.right) {
                queue.push(top.right)
            }            
        }
        if(level%2!=0){
            arr.reverse()
        }
        res.push(arr)
        level++
    }
    return res;
};
console.log(zigzagLevelOrder(root))